import Vue from "vue";
import VueRouter from "vue-router";
import wxShare from "@/assets/js/wxShare.js";

import Login from "../views/home/login.vue";
import BindPhone from "../views/home/bindPhone.vue";
import ChecksyStem from "../views/home/checkSystem.vue";

import searchCenter from "../views/searchCenter/search_center.vue";
import searchdetails from "../views/searchCenter/searchdetails.vue";
import CoachHome from "../views/coach/coachHome.vue";
import CoachImprove from "../views/coach/coachImprove.vue";
import CoachImproveView from "../views/coach/coachImproveView.vue";
import CoachRegister from "../views/coach/coachRegister.vue";

import RefereeHome from "../views/referee/refereeHome.vue";
import RefereeRegister from "../views/referee/refereeRegister.vue";
import RefereeRegisterView from "../views/referee/refereeRegisterView.vue";
import RefereeMemberForm from "../views/referee/refereeMemberForm.vue";
import RefereeMemberView from "../views/referee/refereeMemberView.vue";
//新的完善信息
import currRefereeRegisterpusl from "../views/referee/currRefereeRegisterpusl.vue";

import CoachEnrollList from "../views/enroll/coachEnrollList.vue";
import CoachEnrollDetail from "../views/enroll/coachEnrollDetail.vue";
import coachEnrollApply from "../views/enroll/coachEnrollApply.vue";
import CoachSignList from "../views/enroll/coachSignList.vue";
import CoachSign from "../views/enroll/coachSign.vue";
import CoachSignDetailList from "../views/enroll/coachSignDetailList.vue";
import CoachEnrollHistoryList from "../views/enroll/coachEnrollHistoryList.vue";
import RefereeEnrollList from "../views/enroll/refereeEnrollList.vue";
import RefereeEnrollDetail from "../views/enroll/refereeEnrollDetail.vue";
import RefereeEnrollHistoryList from "../views/enroll/refereeEnrollHistoryList.vue";

import RefereeAnswerIndex from "../views/answer/refereeAnswerIndex.vue";
import RefereeAnswer from "../views/answer/refereeAnswer.vue";

import OrderSuccess from "../views/order/orderSuccess.vue";
import RefereeRegisterOrderDetail from "../views/order/refereeRegisterOrderDetail.vue";
import RefereeRegisterOrderList from "../views/order/refereeRegisterOrderList.vue";
import RefereeRegisterOrderView from "../views/order/refereeRegisterOrderView.vue";
import CoachEnrollOrderDetail from "../views/order/coachEnrollOrderDetail.vue";
import RefereeEnrollOrderDetail from "../views/order/refereeEnrollOrderDetail.vue";

import MainUserCenter from "../views/user/mainUserCenter.vue";
import CoachUserCenter from "../views/user/coachUserCenter.vue";
import RefereeUserCenter from "../views/user/refereeUserCenter.vue";
import MyHonor from "../views/user/myHonor.vue";
import UserInfo from "../views/user/userInfo.vue";
import NoInfoTips from "../views/user/noInfoTips.vue";

//会员
import memberlist from "../views/user/memberlist.vue"; //会员列表 选择系统
import tuantihuiyuan from "../views/user/tuantihuiyuan.vue"; //团体会员
import Memberinformation from "../views/user/Memberinformation.vue"; //团体会员信息
import TeamMemberType from "../views/user/teamMemberType.vue"; //会员列表 选择系统
import OldTeamMemberList from "../views/user/oldTeamMemberList.vue"; //会员列表 选择系统

import Remove from "../views/home/remove.vue";

import courselist from "../views/course/courselist.vue";
import coursevoiel from "../views/course/coursevoiel.vue";
import courelistsyt from "../views/course/courelistsyt.vue";

import enrollstwo from "../views/enrollstwo/index.vue";
import enrollstwowanxin from "../views/enrollstwo/wansxin.vue";
import qrCode from "../views/enrollstwo/qr_code.vue";
import qrCodepusl from "../views/enrollstwo/qrCodepusl.vue";

//裁判员培训报名分享
import enrollsthree from "../views/enrollstwo/enrollsthree.vue";

Vue.use(VueRouter);

// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch((err) => err);
// };

const routes = [
  // home
  {
    path: "/",
    name: "ChecksyStem",
    component: ChecksyStem,
    title: "首页",
  }, // 选择系统页
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      title: "登录",
    },
  }, // 登录
  {
    path: "/bindPhone",
    name: "BindPhone",
    component: BindPhone,
  }, // 绑定手机

  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */

  //公示中心
  {
    path: "/searchCenter",
    name: "searchCenter",
    component: searchCenter,
    meta: {
      title: "公示中心",
    },
  },
  {
    path: "/searchdetails",
    name: "searchdetails",
    component: searchdetails,
    meta: {
      title: "公示中心",
    },
  },
  /*************************************************************************** */

  // 教练员
  {
    path: "/coachCenter",
    name: "CoachHome",
    component: CoachHome,
    meta: {
      title: "教练员",
    },
  }, // 教练员------首页
  {
    path: "/coachCenter/improve",
    name: "CoachImprove",
    component: CoachImprove,
    meta: {
      title: "教练员",
    },
  }, // 教练员------完善信息
  {
    path: "/coachCenter/improve/view",
    name: "CoachImproveView",
    component: CoachImproveView,
  }, // 教练员------查看填写
  {
    path: "/coachCenter/register",
    name: "CoachRegister",
    component: CoachRegister,
    meta: {
      title: "注册",
    },
  }, // 教练员------注册
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 裁判员
  {
    path: "/refereeCenter",
    name: "RefereeHome",
    component: RefereeHome,
    meta: {
      title: "裁判员",
    },
  }, // 裁判员------首页
  {
    path: "/refereeCenter/register",
    name: "RefereeRegister",
    component: RefereeRegister,
    meta: {
      title: "裁判员",
    },
  }, // 裁判员------注册
  {
    path: "/refereeCenter/currRefereeRegisterpusl",
    name: "currRefereeRegisterpusl",
    component: currRefereeRegisterpusl,
    meta: {
      title: "完善信息",
    },
  }, // 裁判员------新的完善
  {
    path: "/refereeCenter/register/view",
    name: "RefereeRegisterView",
    component: RefereeRegisterView,
    meta: {
      title: "查看详情",
    },
  }, // 裁判员------注册订单填写查看
  {
    path: "/refereeCenter/member/join",
    name: "RefereeMemberForm",
    component: RefereeMemberForm,
    meta: {
      title: "会员注册",
    },
  }, // 裁判员------会员注册
  {
    path: "/refereeCenter/member/view",
    name: "RefereeMemberView",
    component: RefereeMemberView,
    meta: {
      title: "注册信息",
    },
  }, // 裁判员------会员注册
  /**************************************************************************** */
  /**************************************************************************** */
  /**************************************************************************** */
  /**************************************************************************** */
  // 报名
  //课程
  {
    path: "/course/courselist",
    name: "courselist",
    component: courselist,
    meta: {
      title: "课程列表",
    },
  },
  {
    path: "/course/courselist/courelistsyt",
    name: "courelistsyt",
    component: courelistsyt,
    // meta: {
    //   title: "课程列表",
    // },
  },
  {
    path: "/course/coursevoiel",
    name: "coursevoiel",
    component: coursevoiel,
    meta: {
      title: "课程视频",
    },
  },

  {
    path: "/enroll/coach/apply_sign",
    name: "coachEnrollApply",
    component: coachEnrollApply,
    meta: {
      title: "培训报名/签到",
    },
  },
  {
    path: "/enroll/coach/List",
    name: "CoachEnrollList",
    component: CoachEnrollList,
    meta: {
      title: "培训报名",
    },
  }, // 教练员------培训报名列表页

  //////////
  {
    path: "/enrollstwo/enrollstwo",
    name: "enrollstwo",
    component: enrollstwo,
    meta: {
      title: "培训报名",
    },
  },
  //裁判员培训报名
  {
    path: "/enrollsthree/enrollthree",
    name: "enrollsthree",
    component: enrollsthree,
    meta: {
      title: "培训报名",
    },
  },
  {
    path: "/enrollstwo/enrollstwowanxin",
    name: "enrollstwowanxin",
    component: enrollstwowanxin,
    meta: {
      title: "完善信息",
    },
  },
  {
    path: "/enrollstwo/qrCode",
    name: "qrCode",
    component: qrCode,
    meta: {
      title: "报名登记",
    },
  },

  {
    path: "/enrollstwo/qrCodepusl",
    name: "qrCodepusl",
    component: qrCodepusl,
    meta: {
      title: "报名登记",
    },
  },
  //////////////////

  {
    path: "/enroll/coach/signList",
    name: "CoachSignList",
    component: CoachSignList,
    meta: {
      title: "签到活动列表",
    },
  },
  {
    path: "/enroll/coach/sign",
    name: "CoachSign",
    component: CoachSign,
    meta: {
      title: "教练员签到",
    },
  },
  {
    path: "/enroll/coach/signDetailList",
    name: "CoachSignDetailList",
    component: CoachSignDetailList,
    meta: {
      title: "教练员签到",
    },
  },
  {
    path: "/enroll/coach/detail",
    name: "CoachEnrollDetail",
    component: CoachEnrollDetail,
    meta: {
      title: "报名详情",
    },
  }, // 教练员------培训报名详情介绍页
  {
    path: "/enroll/coach/history",
    name: "CoachEnrollHistoryList",
    component: CoachEnrollHistoryList,
    meta: {
      title: "我的报名",
    },
  }, // 教练员------我的报名页

  {
    path: "/enroll/referee/List",
    name: "RefereeEnrollList",
    component: RefereeEnrollList,
    meta: {
      title: "培训报名",
    },
  }, // 裁判员------培训报名列表页
  {
    path: "/enroll/referee/detail",
    name: "RefereeEnrollDetail",
    component: RefereeEnrollDetail,
    meta: {
      title: "报名详情",
    },
  }, // 裁判员------培训报名详情介绍页
  {
    path: "/enroll/referee/history",
    name: "RefereeEnrollHistoryList",
    component: RefereeEnrollHistoryList,
    meta: {
      title: "我的报名",
    },
  }, // 裁判员------我的报名页
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 答题
  {
    path: "/answer/referee/index",
    name: "RefereeAnswerIndex",
    component: RefereeAnswerIndex,
    meta: {
      title: "上岗认证",
    },
  }, // 裁判员------上岗认证答题前置页页
  {
    path: "/answer/referee/exam",
    name: "RefereeAnswer",
    component: RefereeAnswer,
    meta: {
      title: "上岗认证",
    },
  }, // 裁判员------上岗认证答题页
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 订单
  {
    path: "/order/success",
    name: "OrderSuccess",
    component: OrderSuccess,
    meta: {
      title: "提示",
    },
  }, // 订单完成提示页
  {
    path: "/order/referee/register/detail",
    name: "RefereeRegisterOrderDetail",
    component: RefereeRegisterOrderDetail,
    meta: {
      title: "支付费用",
    },
  }, // 裁判员------注册订单详情页
  {
    path: "/order/referee/register/List",
    name: "RefereeRegisterOrderList",
    component: RefereeRegisterOrderList,
    meta: {
      title: "注册记录",
    },
  }, // 裁判员------注册订单列表
  {
    path: "/order/referee/register/view",
    name: "RefereeRegisterOrderView",
    component: RefereeRegisterOrderView,
    meta: {
      title: "注册详情",
    },
  }, // 裁判员------注册详情查看
  {
    path: "/order/coach/enroll/detail",
    name: "CoachEnrollOrderDetail",
    component: CoachEnrollOrderDetail,
    meta: {
      title: "订单详情",
    },
  }, // 教练员------培训报名订单详情
  {
    path: "/order/referee/enroll/detail",
    name: "RefereeEnrollOrderDetail",
    component: RefereeEnrollOrderDetail,
    meta: {
      title: "订单详情",
    },
  }, // 裁判员------培训报名订单详情
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  // 用户
  {
    path: "/userCenter",
    name: "MainUserCenter",
    component: MainUserCenter,
    meta: {
      title: "个人中心",
    },
  }, // 总个人中心页
  {
    path: "/coachCenter/userCenter",
    name: "CoachUserCenter",
    component: CoachUserCenter,
    meta: {
      title: "个人中心",
    },
  }, // 教练员-----个人中心页
  {
    path: "/refereeCenter/userCenter",
    name: "RefereeUserCenter",
    component: RefereeUserCenter,
    meta: {
      title: "个人中心",
    },
  }, // 裁判员----个人中心页
  {
    path: "/userHonors",
    name: "MyHonor",
    component: MyHonor,
    meta: {
      title: "我的荣誉",
    },
  }, // 我的荣誉信息页
  {
    path: "/userInfo",
    name: "UserInfo",
    component: UserInfo,
    meta: {
      title: "我的信息",
    },
  }, // 我的信息选择页
  {
    path: "/noInfoTips",
    name: "NoInfoTips",
    component: NoInfoTips,
    meta: {
      title: "我的信息",
    },
  }, // 我的信息提示填写页面

  //会员列表 选择系统
  {
    path: "/memberlist",
    name: "memberlist",
    component: memberlist,
    meta: {
      title: "选择系统",
    },
  },
  {
    path: "/team/membertype",
    name: "TeamMemberType",
    component: TeamMemberType,
    meta: {
      title: "选择团体会员类型",
    },
  },
  {
    path: "/team/oldteammemberlist",
    name: "OldTeamMemberList",
    component: OldTeamMemberList,
    meta: {
      title: "团体会员列表",
    },
  },
  {
    path: "/tuantihuiyuan",
    name: "tuantihuiyuan",
    component: tuantihuiyuan,
    meta: {
      title: "团体会员",
    },
  },

  {
    path: "/Memberinformation",
    name: "Memberinformation",
    component: Memberinformation,
    meta: {
      title: "填写信息",
    },
  },
  // 我的信息提示填写页面

  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  /*************************************************************************** */
  {
    path: "/remove",
    name: "Remove",
    component: Remove,
  }, // 清除缓存
];

const router = new VueRouter({
  mode: "history",
  base: "/coach/",
  routes,
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // console.log(to);

  // 需要登录的页面
  const nextRoute = [
    "ChecksyStem",

    "CoachHome",
    "CoachImprove",
    "CoachImproveView",
    "CoachRegister",

    "RefereeHome",
    "RefereeRegister",
    "currRefereeRegisterpusl", //新的裁判员完善信息
    "RefereeRegisterView",
    "RefereeMemberForm",
    "RefereeMemberView",

    "CoachEnrollList",
    "CoachEnrollDetail",
    "CoachEnrollHistoryList",
    "coachEnrollApply",
    "CoachSignList",
    "CoachSign",
    "CoachSignDetailList",
    "RefereeEnrollList",
    "RefereeEnrollDetail",

    "RefereeAnswerIndex",
    "RefereeAnswer",

    "OrderSuccess",
    "RefereeRegisterOrderDetail",
    "RefereeRegisterOrderList",
    "RefereeRegisterOrderView",
    "CoachEnrollOrderDetail",

    "MainUserCenter",
    "CoachUserCenter",
    "RefereeUserCenter",
    "MyHonor",
    "UserInfo",
    "NoInfoTips",
    "enrollstwo",
    "enrollstwowanxin",
    "searchCenter",
    "searchdetails",
    "enrollsthree",

    "memberlist",
    "tuantihuiyuan",
    "Memberinformation",
    "TeamMemberType",
    "OldTeamMemberList",
  ];
  let isLogin = sessionStorage.getItem("COACH_LOGIN_TOKEN"); // 判断是否登录，本地存储有用户数据则视为已经登录
  let hasPhone = JSON.parse(sessionStorage.getItem("COACH_HAS_PHONE")); // 判断是否绑定手机

  // 未登录状态；当路由到 nextRoute 指定页时，跳转至 登录
  if (nextRoute.indexOf(to.name) >= 0) {
    // 检测是否登录的页面
    if (!isLogin) {
      // 如果未登录（本地存储无用户数据），并且要跳到登录页面
      if (from.name === "Login") {
        console.log("11111111");
        next("/login");
        return;
      } else {
        router.push({
          name: "Login",
          params: {
            redirect: to.fullPath,
          },
        });
        console.log(to.fullPath, "111111188989");
      }
    } else {
      if (!hasPhone) {
        if (from.name === "BindPhone") {
          next("/bindPhone");
          return;
        }

        router.push({
          name: "BindPhone",
        });
      }
    }
  }
  setTimeout(() => {
    let before_Path = router.app.$options.store.state.before_Path;
    console.log(before_Path, "setTimeout");
    if (isLogin) {
      if (to.name == before_Path) {
        console.log("99999999999");
        router.push({
          path: before_Path,
        });
      } else {
        if (to.name == "Login") {
          console.log("22222222222222");
          next("/");
          return;
        }
      }
    }
    // if (before_Path == "/gameCenter/signIn") {
    //   if (isLogin) {
    //     router.push({
    //       name: "signIn",
    //     });
    //   }
    // } else {
    //   if (to.name === "Login") {
    //     if (isLogin) {
    //       next("/");
    //       return;
    //     }
    //   }
    // }
  }, 0);
  // // 已登录状态；当路由到 登录 时，跳转至 首页
  // if (to.name === "Login") {
  //   if (isLogin) {
  //     next("/");
  //     return;
  //   }
  // }
  next(); // 必须使用 next ,执行效果依赖 next 方法的调用参数
});

router.afterEach((to) => {
  wxShare(
    "https://shslqxh.1meeting.com/coach" + to.fullPath,
    "上海市篮球协会数据服务中心",
    "",
    "上海市篮球协会数据服务中心",
    "https://shslqxh.1meeting.com/coach",
    "https://shslqxh.1meeting.com/coach/api/image/common/logo.png"
  );
  const u = navigator.userAgent.toLowerCase();
  if (
    u.indexOf("like mac os x") < 0 ||
    u.match(/MicroMessenger/i) != "micromessenger"
  )
    return;
  if (to.path !== global.location.pathname) {
    // console.log(to);
    // console.log(global.location.pathname);
    // location.assign("/coach" + to.fullPath);
  }
});

export default router;
